//
//  FFWalletCardCell.m
//  LLWallet
//
//  Created by tt on 2020/8/16.
//

#import "FFWalletCardCell.h"

@implementation FFWalletCardModel

- (Class)cell {
    return FFWalletCardCell.class;
}

@end

@interface FFWalletCardCell ()

@property(nonatomic,strong) UIView *cardView;
@property(nonatomic,strong) UILabel *balanceLbl;
@property(nonatomic,strong) UILabel *unitLbl;

@property(nonatomic,strong) UIView *lineView1;
@property(nonatomic,strong) UIView *lineView2;

@property(nonatomic,strong) UIButton *chargeBtn; // 充值按钮
@property(nonatomic,strong) UIButton *withdrawBtn; // 提现按钮

@property(nonatomic,strong) FFWalletCardModel *model;

@end

@implementation FFWalletCardCell

#define cellHeight 150.0f
+(CGSize) sizeForModel:(WKFormItemModel*)model {
    return CGSizeMake(WKScreenWidth, cellHeight);
}

- (void)setupUI {
    [super setupUI];
    [self setBackgroundColor:[UIColor clearColor]];
    
    [self.contentView addSubview:self.cardView];
    [self.cardView addSubview:self.balanceLbl];
    [self.cardView addSubview:self.unitLbl];
    [self.cardView addSubview:self.lineView1];
    [self.cardView addSubview:self.lineView2];
    [self.cardView addSubview:self.chargeBtn];
    [self.cardView addSubview:self.withdrawBtn];

}

- (UIView *)cardView {
    if(!_cardView) {
        CGRect rect = CGRectMake(15.0f, 0.0f, WKScreenWidth - 30.0f,cellHeight);
        _cardView = [[UIView alloc] initWithFrame:rect];
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0.0f, 0.0f, rect.size.width,rect.size.height);
        gl.startPoint = CGPointMake(0.5, 0);
        gl.endPoint = CGPointMake(0.5, 1);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:230/255.0 green:185/255.0 blue:98/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:213/255.0 green:158/255.0 blue:50/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [_cardView.layer addSublayer:gl];
        _cardView.layer.masksToBounds = YES;
        _cardView.layer.cornerRadius = 8.0f;
    }
    return _cardView;
}

- (UILabel *)balanceLbl {
    if(!_balanceLbl) {
        _balanceLbl = [[UILabel alloc] init];
        [_balanceLbl setFont:[UIFont fontWithName:@"Avenir-Medium" size:24.0f]];
        [_balanceLbl setTextColor:[UIColor whiteColor]];
    }
    return _balanceLbl;
}

- (UILabel *)unitLbl {
    if(!_unitLbl) {
        _unitLbl = [[UILabel alloc] init];
        _unitLbl.text = LLang(@"余额(元)");
        [_unitLbl sizeToFit];
        [_unitLbl setTextColor:[UIColor colorWithRed:255.0f/255.0f green:222.0f/255.0f blue:158.0f/255.0f alpha:1.0f]];
        [_unitLbl setFont:[[WKApp shared].config appFontOfSize:14.0f]];
    }
    return _unitLbl;
}

- (UIView *)lineView1 {
    if(!_lineView1) {
        _lineView1 = [[UIView alloc] init];
        _lineView1.lim_height = 1.0f;
        [_lineView1 setBackgroundColor:[UIColor colorWithRed:229/255.0 green:178/255.0 blue:78/255.0 alpha:1.0]];
    }
    return _lineView1;
}

- (UIView *)lineView2 {
    if(!_lineView2) {
        _lineView2 = [[UIView alloc] init];
        _lineView2.lim_height = 20.0f;
        _lineView2.lim_width = 1.0f;
        [_lineView2 setBackgroundColor:[UIColor colorWithRed:229/255.0 green:178/255.0 blue:78/255.0 alpha:1.0]];
    }
    return _lineView2;
}

- (UIButton *)chargeBtn {
    if(!_chargeBtn) {
        _chargeBtn = [[UIButton alloc] init];
        [_chargeBtn setTitle:LLang(@"充值") forState:UIControlStateNormal];
        [_chargeBtn setTitleColor:[UIColor colorWithRed:255.0f/255.0f green:222.0f/255.0f blue:158.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        [[_chargeBtn titleLabel] setFont:[[WKApp shared].config appFontOfSize:16.0f]];
        [_chargeBtn addTarget:self action:@selector(onRecharge) forControlEvents:UIControlEventTouchUpInside];
    }
    return _chargeBtn;
}

- (UIButton *)withdrawBtn {
    if(!_withdrawBtn) {
        _withdrawBtn = [[UIButton alloc] init];
        [_withdrawBtn setTitle:LLang(@"提现") forState:UIControlStateNormal];
        [_withdrawBtn setTitleColor:[UIColor colorWithRed:255.0f/255.0f green:222.0f/255.0f blue:158.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        [[_withdrawBtn titleLabel] setFont:[[WKApp shared].config appFontOfSize:16.0f]];
        [_withdrawBtn addTarget:self action:@selector(onWithdraw) forControlEvents:UIControlEventTouchUpInside];
    }
    return _withdrawBtn;
}

// 充值点击
-(void) onRecharge {
    if(self.model.onRecharge) {
        self.model.onRecharge();
    }
}

// 提现点击
-(void) onWithdraw {
    if(self.model.onWithdraw) {
        self.model.onWithdraw();
    }
}

- (void)refresh:(FFWalletCardModel *)model {
    [super refresh:model];
    self.model = model;
    
    self.balanceLbl.text = [WKMoneyUtil penny2YuanFormat:model.amount];
    [self.balanceLbl sizeToFit];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.balanceLbl.lim_top = 25.0f;
    self.balanceLbl.lim_left = self.cardView.lim_width/2.0f - self.balanceLbl.lim_width/2.0f;
    
    self.unitLbl.lim_top = self.balanceLbl.lim_bottom;
    self.unitLbl.lim_left = self.cardView.lim_width/2.0f - self.unitLbl.lim_width/2.0f;
    
    self.lineView1.lim_width = self.cardView.lim_width;
    self.lineView1.lim_top = 100.0f;
    
    CGFloat bottomHeight = self.cardView.lim_height - self.lineView1.lim_bottom;
    self.lineView2.lim_left = self.cardView.lim_width/2.0f - self.lineView2.lim_width/2.0f;
    self.lineView2.lim_top = self.lineView1.lim_bottom + bottomHeight/2.0f - self.lineView2.lim_height/2.0f;
    
    self.chargeBtn.lim_width = self.cardView.lim_width/2.0f - 0.5f;
    self.chargeBtn.lim_height = bottomHeight;
    self.chargeBtn.lim_top = self.cardView.lim_height - bottomHeight;
    
    self.withdrawBtn.lim_width = self.cardView.lim_width/2.0f - 0.5f;
    self.withdrawBtn.lim_height =bottomHeight;
    self.withdrawBtn.lim_top = self.cardView.lim_height - bottomHeight;
    self.withdrawBtn.lim_left = self.lineView2.lim_right;
}


@end
